package node.appearance;

import java.util.*;
import javax.media.j3d.*;

public class AppearanceFactory {

	public static final String SOLID_STATE = "solid";
	public static final String TRANSPARENT_STATE = "trans";
	public static final String INVISIBLE_STATE = "invisible";
	
	private TransparencyAttributes transparency = null;
	private TransparencyAttributes invisible = null;
	
	private final Map<String, Appearance> appearances = new HashMap<String, Appearance>();
	private final Map<String, Material> materials = new HashMap<String, Material>();
	
	public AppearanceFactory() {
		transparency = new TransparencyAttributes();
		transparency.setTransparencyMode(TransparencyAttributes.BLENDED);
		transparency.setTransparency(0.3f);
		invisible = new TransparencyAttributes();
		invisible.setTransparencyMode(TransparencyAttributes.BLENDED);
		invisible.setTransparency(1f);
	}
	
	public Appearance getAppearance(String state, String material) {
		Appearance ap = appearances.get(state + material);
		if (ap != null) return ap;
		
		ap = new Appearance();
		if (state.equals(INVISIBLE_STATE)) {
			ap.setTransparencyAttributes(invisible);
		}
		if (state.equals(TRANSPARENT_STATE)) {
			ap.setTransparencyAttributes(transparency);
		}
		ap.setMaterial(getMaterial(material));
		appearances.put(state + material, ap);
		
		return ap;
	}
	
	public void registerMaterial(String id, Material material) {
		materials.put(id, material);
	}
	
	protected Material getMaterial(String material) {
		Material mat = materials.get(material);
		if (mat == null) {
			mat = new Material();
			System.err.println("No material called: " + material);
			materials.put(material, mat);
		}
		
		return mat;
	}
	
}
